import os
import xml.etree.ElementTree as ET
from openpyxl import Workbook

def parse_pom_file(pom_file_path):
    tree = ET.parse(pom_file_path)
    root = tree.getroot()

    group_id = None
    artifact_id = None
    version = None

    for child in root:
        if child.tag == 'groupId':
            group_id = child.text
        elif child.tag == 'artifactId':
            artifact_id = child.text
        elif child.tag =='version':
            version = child.text

    return group_id, artifact_id, version

if __name__ == "__main__":
    target_directory = r'E:\zx\gzda\com\sugar\credit'

    pom_files = []
    for root, dirs, files in os.walk(target_directory):
        for file in files:
            if file.endswith('.pom'):
                pom_files.append(os.path.join(root, file))

    workbook = Workbook()
    sheet1 = workbook.active
    sheet1.title = "GroupIds"
    sheet2 = workbook.create_sheet(title="ArtifactIds")
    sheet3 = workbook.create_sheet(title="Versions")

    row_num = 1
    for pom_file in pom_files:
        group_id, artifact_id, version = parse_pom_file(pom_file)
        sheet1.cell(row=row_num, column=1, value=group_id if group_id else "无对应标签")
        sheet2.cell(row=row_num, column=1, value=artifact_id if artifact_id else "无对应标签")
        sheet3.cell(row=row_num, column=1, value=version if version else "无对应标签")
        row_num += 1

    workbook.save('pom_info.xlsx')